//
//  YZPersonTableViewController.m
//  个人详情控制器
//
//  Created by yz on 15/8/13.
//  Copyright (c) 2015年 yz. All rights reserved.
//

#import "FWPersonTableViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "UIImage+Extension.h"

#define FWHeadViewH 200

#define FWHeadViewMinH 64

#define FWTabBarH 44

@interface FWPersonTableViewController ()
@property (nonatomic, assign) CGFloat lastOffsetY;
@end

@implementation FWPersonTableViewController

-(void)setHeadHCons:(NSLayoutConstraint *)headHCons{
    _headHCons = headHCons;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _lastOffsetY = -(FWHeadViewH + FWTabBarH);
    
    // 设置顶部额外滚动区域
    self.tableView.contentInset = UIEdgeInsetsMake(FWHeadViewH + FWTabBarH , 0, 0, 0);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 获取当前偏移量
    CGFloat offsetY = scrollView.contentOffset.y;
    
    // 获取偏移量差值
    CGFloat delta = offsetY - _lastOffsetY;
    
    CGFloat headH = FWHeadViewH - delta;
    
    if (headH < FWHeadViewMinH) {
        headH = FWHeadViewMinH;
    }
    
    _headHCons.constant = headH;
    
    // 计算透明度，刚好拖动200 - 64 136，透明度为1
    
    CGFloat alpha = delta / (FWHeadViewH - FWHeadViewMinH);
    
    // 获取透明颜色
    UIColor *alphaColor = [UIColor colorWithWhite:0 alpha:alpha];
    [_titleLabel setTextColor:alphaColor];
    
    // 设置导航条背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:1 alpha:alpha]] forBarMetrics:UIBarMetricsDefault];
}
@end
